package com.example.personal.mvvm.activity

import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.personal.R
import com.example.personal.mvvm.adapter.MyAttentionAdapter
import com.example.personal.mvvm.bean.AttentionBean
import com.example.personal.mvvm.viewmodel.AttentionViewModel
import com.gsafety.libraryktx.base.BaseActivity
import com.gsafety.libraryktx.bean.LoadState
import com.gsafety.libraryktx.core.RouterConstants
import com.gsafety.libraryktx.expand.create
import com.gsafety.libraryktx.expand.toast
import com.gsafety.libraryktx.util.RecyclerViewHelper
import com.gsafety.libraryktx.widget.CommonRefreshView
import kotlinx.android.synthetic.main.personal_attention_list_layout.*

/**我的关注
 * @author : lj
 * @date : 2020/12/29 16:22
 */
@Route(path = RouterConstants.MY_ATTENTION_LIST_ACTIVITY)
class MyAttentionListActivity: BaseActivity(), CommonRefreshView.RefreshListener {
    private var mAdapter: MyAttentionAdapter? = null
    private var datas: MutableList<AttentionBean.RecordsBean> = ArrayList()
    private lateinit var mv: AttentionViewModel
    var pageSize = 10
    var current = 1
    var currentAttendPos = 0
    var mEtSearch: EditText? = null

    override fun initView() {
        mv = AttentionViewModel()
        mv = create(AttentionViewModel::class)
        var title=findViewById<TextView>(R.id.title_name)
        title.text=resources.getText(R.string.personal_my_attention)
        crv_common_refresh.mRefreshListener = this
        mEtSearch = findViewById<EditText>(R.id.et_search)
    }

    override fun initData() {
        var rv_msg=findViewById<RecyclerView>(R.id.rv_msg)
        mAdapter = MyAttentionAdapter(datas)
        RecyclerViewHelper.initRecyclerViewV(this, rv_msg!!, false, mAdapter)

        mv.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Success -> {
                    crv_common_refresh.finishRefresh()
                    mv.loadValue.value.let {it->
                        datas.addAll(it?.records!!)
                        mAdapter?.setNewData(datas)
                    }
                }
                is LoadState.Fail -> {
                    toast(it.msg)
                    crv_common_refresh.loadErrorData()
                }
            }
        })

        mEtSearch?.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                onRefresh()
            }
            false
        }

        mAdapter?.onItemAttentionClickListener = object:MyAttentionAdapter.OnItemAttentionClickListener{
            override fun onItemAttendClick(pos: Int) {
                var item = datas.get(pos)
                currentAttendPos = pos
                mv.cancelAttention(item.attId!!)
            }
        }

        mv.cancelAttendValue.observe(this, Observer {
            if (mv.cancelAttendValue.value == true) {
                onRefresh()
                Toast.makeText(this, "取消关注成功！", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "取消关注失败！", Toast.LENGTH_SHORT).show()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        crv_common_refresh.autoRefresh()
    }

    override fun onRefresh() {
        current = 1
        datas.clear()
        mv.requestWarnList(pageSize,current,mEtSearch?.text.toString())
    }

    override fun onLoadMore() {
        current++
        mv.requestWarnList(pageSize,current,mEtSearch?.text.toString())
    }

    override fun getLayoutId(): Int {
        return R.layout.personal_attention_list_layout
    }
}